package com.topc.others.api;

import com.topc.base.api.CommonResult;
import com.topc.base.model.PageParams;
import com.topc.base.model.PageResult;
import com.topc.others.model.dto.SaveArticleCommentDto;
import com.topc.others.model.dto.SaveArticleInfoDto;
import com.topc.others.model.po.ArticleCommentInfo;
import com.topc.others.model.vo.ArticleCommentShowVo;
import com.topc.others.model.vo.ArticleDetailShowVo;
import com.topc.others.model.vo.ArticleShowVo;
import com.topc.others.service.IArticleCommentInfoService;
import com.topc.others.service.IArticleContentService;
import com.topc.others.service.IArticleInfoService;
import com.topc.others.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/close/article")
public class ArticleCloseController {


    @Autowired
    private IArticleInfoService articleInfoService;

    @Autowired
    private IArticleContentService articleContentService;

    @Autowired
    private IArticleCommentInfoService articleCommentInfoService;

    //文章信息-增添
    @PostMapping("/save")
    public CommonResult<String> saveArticleInfo(@RequestBody @Validated SaveArticleInfoDto saveArticleInfoDto){
        saveArticleInfoDto.setUserNumber(SecurityUtil.getUser().getNumber());
        articleInfoService.saveArticleInfo(saveArticleInfoDto);
        return CommonResult.success("新增成功");
    }
    //文章信息-修改
    @PostMapping("/update-info")
    public CommonResult<String> updateInfoById(@NotNull Long id,@RequestBody @Validated SaveArticleInfoDto saveArticleInfoDto){
        saveArticleInfoDto.setUserNumber(SecurityUtil.getUser().getNumber());
        articleInfoService.updateInfoById(id,saveArticleInfoDto);
        return CommonResult.success("修改成功");
    }
    //文章信息-删除
    @GetMapping("/delete")
    public CommonResult<String> deleteInfoById(@NotNull Long id){
        //通过id找到文章内容id，并删除文章内容
        articleInfoService.deleteInfoById(id);
        //删除文章基本信息
        return CommonResult.success("删除成功");
    }

    /**
     * TODO
     * @param pageParams
     * @param key
     * @return
     */
    //文章信息查看
    @PostMapping("/search-info")
    public CommonResult<PageResult<ArticleShowVo>> searchInfoByPage(PageParams pageParams, @NotNull String key){
        PageResult<ArticleShowVo> pageResult = articleInfoService.searchByPage(pageParams, key);
        return CommonResult.success(pageResult);
    }

    /**
     * TODO
     * @param id
     * @return
     */
    @GetMapping("/get-detail-by-id")
    public CommonResult<ArticleDetailShowVo> getInfoDetailById(@NotNull Long id){
        ArticleDetailShowVo articleDetailShowVo = articleInfoService.searchDetailById(id);
        return CommonResult.success(articleDetailShowVo);
    }

    //文章评论查看
    @PostMapping("/search-comment")
    public CommonResult<PageResult<ArticleCommentShowVo>> searchCommentByNumber(PageParams pageParams, @NotNull String userNumber){
        PageResult<ArticleCommentShowVo> pageResult = articleInfoService.searchCommentByNumber(pageParams, userNumber);
        return CommonResult.success(pageResult);
    }

    //增加评论
    @PostMapping("/comment/save")
    public CommonResult<String> saveCommentInfo(@RequestBody @Validated SaveArticleCommentDto saveArticleCommentDto){
        saveArticleCommentDto.setUserNumber(SecurityUtil.getUser().getNumber());
        articleInfoService.saveCommentInfo(saveArticleCommentDto);
        return CommonResult.success("评论成功");
    }

    //删除评论
    @GetMapping("/comment/delete")
    public CommonResult<String> deleteCommentById(@NotNull Long id){
        articleCommentInfoService.deleteCommentInfoById(id);
        return CommonResult.success("删除成功");
    }
}
